package com.wcsy.myblog.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcsy.myblog.dao.ArticleMapper;
import com.wcsy.myblog.dao.CommentMapper;
import com.wcsy.myblog.dao.UserMapper;
import com.wcsy.myblog.entity.Article;
import com.wcsy.myblog.entity.Comment;
import com.wcsy.myblog.entity.User;
import com.wcsy.myblog.service.ICommentService;
import org.apache.coyote.http11.Http11AprProtocol;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements ICommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    UserMapper  userMapper;
    @Autowired
    HttpSession session;

    @Override
    public List<Comment> listComment(String username) {
        return commentMapper.listComment(username);
    }

    @Override
    public List<Comment> selectOneComment(int articleId) {

        return commentMapper.selectOneComment(articleId);
    }

    @Override
    public boolean insertComment(Comment comment) {
        //根据文章id查询出用户ID
        Article article = articleMapper.selectOneArticle((int) comment.getArticleId());
        //添加评论
        boolean b = commentMapper.insertComment(comment);
        // 添加未显示的评论值
//        User user = userMapper.selectOne(new QueryWrapper<User>().eq("user_id", 1));
        User user = (User) session.getAttribute("user");
        user.setUserComment(user.getUserComment()+1);
        user.setUserId(article.getUserId());
        int user_id = userMapper.update(user, new QueryWrapper<User>().eq("user_id", article.getUserId()));
        return b;
    }
    @Override
    public void deleteComment(int commentId) {
        //根据评论id查出文章id
        Comment comment = commentMapper.selectComment(commentId);
        //根据文章id查出用户id
        Article article = articleMapper.selectOneArticle((int) comment.getArticleId());
        // 删除未显示的评论值
        User user = new User();
        user.setUserComment(user.getUserComment()-1);
        int user_id = userMapper.update(user, new QueryWrapper<User>().eq("user_id", article.getUserId()));
        commentMapper.deleteComment(commentId);
    }
    @Override
    public List<Comment> MyListComment(int userId) {
        return commentMapper.MyListComment(userId);
    }

    @Override
    public List<Comment> thisListComment(int userId1) {
        return commentMapper.thisListComment(userId1);
    }
}
